package com.example.demo.controller;

import com.zhucan.enums.scanner.handler.EnumScanHandler;
import com.zhucan.enums.scanner.model.CodeTable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 枚举码表控制器
 * 
 * 演示功能：
 * 1. 获取所有枚举码表数据
 * 2. 前端可以通过这些接口获取下拉选项数据
 * 3. 枚举扫描和缓存功能展示
 */
@RestController
@RequestMapping("/api/enums")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "枚举码表", description = "获取系统中所有枚举的码表数据")
public class EnumController {
    
    private final EnumScanHandler enumScanHandler;
    
    /**
     * 获取所有枚举码表
     * 这是框架的核心功能展示接口
     */
    @GetMapping
    @ApiOperation(value = "获取所有枚举码表", notes = "返回系统中所有标注了@EnumScan的枚举数据，前端可用于生成下拉选项")
    public ResponseEntity<List<CodeTable>> getAllEnums() {
        log.info("获取所有枚举码表");
        
        List<CodeTable> codeTables = enumScanHandler.codeTables();
        
        log.info("成功获取枚举码表，共{}个枚举", codeTables.size());
        
        return ResponseEntity.ok(codeTables);
    }
    
    /**
     * 根据枚举名称获取特定枚举码表
     */
    @GetMapping("/{enumName}")
    @ApiOperation(value = "根据枚举名称获取码表", notes = "获取指定枚举的码表数据")
    public ResponseEntity<CodeTable> getEnumByName(@PathVariable String enumName) {
        log.info("获取指定枚举码表：{}", enumName);
        
        List<CodeTable> codeTables = enumScanHandler.codeTables();
        CodeTable targetTable = codeTables.stream()
                .filter(table -> enumName.equals(table.getEnumName()))
                .findFirst()
                .orElse(null);
        
        if (targetTable == null) {
            return ResponseEntity.notFound().build();
        }
        
        return ResponseEntity.ok(targetTable);
    }
    
    /**
     * 根据分类获取枚举码表
     */
    @GetMapping("/category/{category}")
    @ApiOperation(value = "根据分类获取枚举码表", notes = "获取指定分类下的所有枚举")
    public ResponseEntity<List<CodeTable>> getEnumsByCategory(@PathVariable String category) {
        log.info("根据分类获取枚举码表：{}", category);
        
        List<CodeTable> codeTables = enumScanHandler.codeTables();
        List<CodeTable> categoryTables = codeTables.stream()
                .filter(table -> {
                    // 这里需要根据实际的CodeTable实现来判断分类
                    // 假设分类信息在description或其他字段中
                    return table.getEnumName().toLowerCase().contains(category.toLowerCase());
                })
                .collect(java.util.stream.Collectors.toList());
        
        return ResponseEntity.ok(categoryTables);
    }
    
    /**
     * 刷新枚举缓存
     */
    @PostMapping("/refresh")
    @ApiOperation(value = "刷新枚举缓存", notes = "管理员接口，重新扫描和缓存枚举数据")
    public ResponseEntity<String> refreshEnumCache() {
        log.info("刷新枚举缓存");
        
        try {
            // 如果EnumScanHandler有refresh方法的话
            // enumScanHandler.refresh();
            
            List<CodeTable> codeTables = enumScanHandler.codeTables();
            
            log.info("枚举缓存刷新成功，共{}个枚举", codeTables.size());
            
            return ResponseEntity.ok("枚举缓存刷新成功，共 " + codeTables.size() + " 个枚举");
        } catch (Exception e) {
            log.error("刷新枚举缓存失败", e);
            return ResponseEntity.internalServerError().body("刷新枚举缓存失败：" + e.getMessage());
        }
    }
}
